package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.FriendNews;
import cn.wolfcode.trip.base.domain.FriendNewsComment;
import cn.wolfcode.trip.base.domain.FriendNewsLiker;
import cn.wolfcode.trip.base.mapper.FriendNewsCommentMapper;
import cn.wolfcode.trip.base.mapper.FriendNewsLikerMapper;
import cn.wolfcode.trip.base.mapper.FriendNewsMapper;
import cn.wolfcode.trip.base.service.IFriendNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class FriendNewsServiceImpl implements IFriendNewsService {

    @Autowired
    private FriendNewsMapper friendNewsMapper;
    @Autowired
    private FriendNewsLikerMapper friendNewsLikerMapper;
    @Autowired
    private FriendNewsCommentMapper friendNewsCommentMapper;

    @Override
    public void save(FriendNews friendNews,Long[] aitedfriendIds,Long[] pointedfriendIds) {
        friendNews.setReleaseTime(new Date());
        friendNewsMapper.insert(friendNews);
        if(aitedfriendIds!=null){
            for (Long aitedfriendId : aitedfriendIds) {
                friendNewsMapper.insertFriendnewsAited(friendNews.getId(),aitedfriendId);
            }
        }
        if (pointedfriendIds!=null){
            for (Long pointedfriendId : pointedfriendIds) {
                friendNewsMapper.insertFriendnewsPointed(friendNews.getId(),pointedfriendId);
            }
        }
    }

    @Override
    public void likerForFriendNews(FriendNewsLiker friendNewsLiker) {
        FriendNewsLiker liker=friendNewsLikerMapper.selectFriendLikerByLikerAndFriendNews(friendNewsLiker);
        if(liker==null){
            friendNewsLiker.setLikeDate(new Date());
            friendNewsLikerMapper.insert(friendNewsLiker);
        }else {
            friendNewsLikerMapper.deleteFriendLikerByLikerAndFriendNews(friendNewsLiker);
        }
    }

    @Override
    public void commentForFriendNews(FriendNewsComment friendNewsComment) {
        friendNewsComment.setCommentDate(new Date());
        friendNewsCommentMapper.insert(friendNewsComment);


    }

    @Override
    public Long selectFriendNewsPublishPeopleIdByFriendNewsId(Long id) {
        return friendNewsMapper.selectFriendNewsPublishPeopleIdByFriendNewsId(id);
    }
}
